/*
 * House Model
 * @Author: chiangbt@geoportal.cn 
 * @Date: 2019-05-19 01:43:49 
 * @Last Modified by: chiangbt@geoportal.cn
 * @Last Modified time: 2019-05-19 12:32:38
 */
const mongoose = require('mongoose')
const uniqueValidator = require('mongoose-unique-validator');

const houseSchema = new mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId, // MongoDB记录中的id项
    name: { // name
        type: String,
        required: true,
        unique: true
    },
    geometry: {
        type: {
            type: String,
            enum: ['Polygon'], //'geometry.type' 必须是几何类型中的一种
            required: true
        },
        coordinates: {
            type: [
                [
                    [Number]
                ]
            ],
            required: true
        }
    }
}, {
    versionKey: false,
    timestamps: {
        createdAt: 'createdAt',
        updatedAt: 'updatedAt'
    }
});


houseSchema.index({
    geometry: "2dsphere"
});

houseSchema.plugin(uniqueValidator);
module.exports = mongoose.model('House', houseSchema);